package com.mt.roll.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.mt.roll.core.exception.ApiException;

import java.io.IOException;

/**
 * <p>
 *     微信带参二维码工具类
 * </p>
 *
 * @author Mr.ZhangShi
 * @date 2019/4/9
 * @since 1.0
 */
public class WechatQRCodeUtil {

    /**
     * 获取access_token
     */
    private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={SECRET}";

    /**
     * 获取ticket
     */
    private static final String GET_QRCODE_URL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={TOKEN}";

    /**
     * 换取二维码
     */
    private static final String QR_CODE_URL = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={TICKET}";

    public static String getAccessToken(String appId, String secret) {
        if (StringUtils.isAnyEmpty(appId, secret)){
            throw new ApiException(new NullPointerException());
        }
        String accessToken = null;
        String url = GET_ACCESS_TOKEN_URL.replace("{APPID}", appId).replace("{SECRET}", secret);
        try {
            String jsonStr = HttpClient.httpGet(url);
            if (StringUtils.isNotEmpty(jsonStr)) {
                accessToken = JSONObject.parseObject(jsonStr).get("access_token").toString();
            }
        } catch (IOException e) {
            throw new ApiException(e);
        }
        return accessToken;
    }

    private static String getTicket(String accessToken, Object scene){
        if (StringUtils.isEmpty(accessToken) || scene == null){
            throw new ApiException(new NullPointerException());
        }
        String ticket = null;
        String url = GET_QRCODE_URL.replace("{TOKEN}", accessToken);
        String param;
        if (scene instanceof Integer) {
            param = "{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": "+ scene + "}}}";
        } else {
            param = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": "+ scene.toString() + "}}}";
        }
        try {
            String jsonStr = HttpClient.httpPost(url, param);
            if (StringUtils.isNotEmpty(jsonStr)) {
                System.out.println("二维码申请成功:" + jsonStr);
                ticket = JSONObject.parseObject(jsonStr).get("ticket").toString();
            }
        } catch (IOException e) {
            throw new ApiException(e);
        }
        return ticket;
    }

    public static String getQrCodePictureUrl(String appId, String secret, Object scene) {
        String ticket = getTicket(getAccessToken(appId, secret), scene);
        if (StringUtils.isNotEmpty(ticket)) {
            return QR_CODE_URL.replace("{TICKET}", ticket);
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getQrCodePictureUrl("wx54c4b46cfc8a9aba", "5ba1210f044a20cdc9bfbc59b582c259", 1154));
    }

}
